const express = require('express');
const router = express.Router();
const DB = require('../DB');
const sql = require('../sqlMap');

// 更新感染人数
router.post('/updateInfected', (req, res) => {
    let sqlStr = sql.school.updateInfected;
    let params = req.body;
    let conn = new DB().getConn();
    conn.query(sqlStr, [params.Infected, params.SchoolName], (err, result) => {
        if (err) {
            res.json(err);
        } else {
            res.json(result);
        }
    });
    conn.end();
});

// 查询感染人数
router.post('/selectInfected', (req, res) => {
    let sqlStr = sql.school.selectInfected;
    let params = req.body;
    let conn = new DB().getConn();
    conn.query(sqlStr, [params.SchoolName], (err, result) => {
        if (err) {
            res.json(err);
        } else {
            console.log(result);
            res.json(result)
        }
    });
    conn.end();
});

// 更新剩余物资
router.post('/updateAllMaterial', (req, res) => {
    let sqlStr = sql.material.updateAllMaterial;
    let params = req.body;
    let conn = new DB().getConn();
    conn.query(sqlStr, [params.Num, params.Name], (err, result) => {
        if (err) {
            res.json(err);
        } else {
            res.json(result);
        }
    });
    conn.end();
});

// 查询剩余物资
router.post('/selectAllMaterial', (req, res) => {
    let sqlStr = sql.material.selectAllMaterial;
    let params = req.body;
    let conn = new DB().getConn();
    conn.query(sqlStr, [params.Name], (err, result) => {
        if (err) {
            res.json(err);
        } else {
            console.log(result);
            res.json(result)
        }
    });
    conn.end();
});

// 更新物资分配
router.post('/updateMaterial', (req, res) => {
    let sqlStr = sql.sch_ma.updateMaterial;
    let params = req.body;
    let conn = new DB().getConn();
    conn.query(sqlStr, [params.Num, params.SchName, params.MaName], (err, result) => {
        if (err) {
            res.json(err);
        } else {
            res.json(result);
        }
    });
    conn.end();
});

// 查询物资分配
router.post('/selectMaterial', (req, res) => {
    let sqlStr = sql.sch_ma.selectMaterial;
    let params = req.body;
    let conn = new DB().getConn();
    conn.query(sqlStr, [params.SchName,params.MaName], (err, result) => {
        if (err) {
            res.json(err);
        } else {
            console.log(result);
            res.json(result)
        }
    });
    conn.end();
});

module.exports = router;
